﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperFox.SuperServiceReference;
using System.Collections.ObjectModel;

namespace SuperFox
{
    public partial class clientOrderReview : UserControl
    {
        private SuperServiceClient client;

        private int OrderItemNumber;

        private ObservableCollection<DbProducts> tempProds;

        private ShoppingCartCollection tempSc;

        public clientOrderReview()
        {
            InitializeComponent();

            this.DataContext = (App.Current.RootVisual as MainPage).CurrentUser;

        }

        private void PlaceButton_Click(object sender, RoutedEventArgs e)
        {
            OrderItemNumber = 0;
            PlaceButton.IsEnabled = false;
            // collect the last bits of the information

            (App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo.MarchantId = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_id;
            (App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo.TotalPostageCost = 0;
            (App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo.TotalProductCost = (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart.GetTotalCost();
            (App.Current.RootVisual as MainPage).CurrentUser.UserOrderInfo.TotalCost = (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart.GetTotalCost();
            

            //PlaceButton the order
            client = ServiceConfig.Connect();

            //client.PlaceOrderCompleted += new EventHandler<PlaceOrderCompletedEventArgs>(client_PlaceOrderCompleted);
            client.AddTempUserCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_AddTempUserCompleted);
            client.AddItemToShoppingTempShoppingCartCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_AddItemToShoppingTempShoppingCartCompleted);
            client.DoOrderCompleted += new EventHandler<DoOrderCompletedEventArgs>(client_DoOrderCompleted);

            

            tempProds = new ObservableCollection<DbProducts>();

            tempSc = new ShoppingCartCollection();

            foreach (DbProducts p in (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products)
            {
                tempProds.Add(p);
            }

            foreach (DbProducts p in (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart)
            {
                tempSc.Add(p);
            }

            (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products = null;

            (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart.Clear();

            client.AddTempUserAsync((App.Current.RootVisual as MainPage).CurrentUser);

            //client.PlaceOrderAsync((App.Current.RootVisual as MainPage).CurrentUser, (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart);

            //client.AddItemToShoppingTempShoppingCartAsync(tempProds[this.OrderItemNumber], ConfigTempProductPlace.First);

            //(App.Current.RootVisual as MainPage).CurrentUser.Programs.Products = tempProds;

            

            //client.PlaceOrderAsync(new DbUsers(), (App.Current.RootVisual as MainPage).CurrentUser.ShoppingCart);
            //(App.Current.RootVisual as MainPage).LoadContent(new clientFinalScreen());
        }

        void client_DoOrderCompleted(object sender, DoOrderCompletedEventArgs e)
        {
            clientFinalScreen cfs = new clientFinalScreen();

            if (e.Error != null)
            {
                cfs.OrderNumberTb.Text = e.Error.Message;
                PlaceButton.IsEnabled = true;
                return;
            }

            cfs.OrderNumberTb.Text = e.Result.ToString();

            (App.Current.RootVisual as MainPage).LoadContent(cfs);

            // clean up code
            (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products = tempProds;


        }

        void client_AddTempUserCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            client.AddItemToShoppingTempShoppingCartAsync(tempSc[this.OrderItemNumber], (App.Current.RootVisual as MainPage).CurrentUser.log_username);
        }


        void client_AddItemToShoppingTempShoppingCartCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            this.OrderItemNumber++;

            if (this.OrderItemNumber == tempSc.Count)
            {
                // we have added all the products to the temp list
                this.OrderItemNumber = 0;

                client.DoOrderAsync((App.Current.RootVisual as MainPage).CurrentUser.log_username);

            }
            else
            { 
                client.AddItemToShoppingTempShoppingCartAsync(tempSc[this.OrderItemNumber], (App.Current.RootVisual as MainPage).CurrentUser.log_username);
            }
        }

        /// <summary>
        /// display the id of the order once the order has been written to the database and emailed to the clients
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //void client_AddTempUserCompleted(object sender, AddTempUserCompletedEventArgs e)
        //{
            
        //}



        private void ReviewShoppingCart_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as clientShoppingCart).NextButton.Visibility = System.Windows.Visibility.Collapsed;
            (sender as clientShoppingCart).BackButton.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            (App.Current.RootVisual as MainPage).LoadContent(new clientOrderForm());
        }
    }
}
